/**
 * File Name   : NewInfoAction.java
 * Description : 
 */
package com.hh.admin.model.newinfo.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.common.service.CommonService;
import com.hh.admin.model.newinfo.form.NewInfoForm;
import com.hh.admin.model.newinfo.form.ParamNewInfoBean;
import com.hh.admin.model.newinfo.form.ResultNewInfoBean;
import com.hh.admin.model.newinfo.service.NewInfoService;
import com.hh.common.bean.PortalLocalBean;
import com.hh.common.consts.AdminConst;

/**
 * 新鲜资讯Action
 * @author 冯昌硕
 * @create 2013-1-20 下午2:34:27
 */
public class NewInfoAction extends MappingDispatchAction {

    /**
     * log
     */
    private static final Logger log = Logger.getLogger(NewInfoAction.class);
    
    /**
     * 公共Service
     */
    private CommonService commonService;
    
    private NewInfoService newInfoService;
    
    /**
     * 获取新鲜资讯内容列表
     * @create 2013-1-20 下午4:56:08
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        NewInfoForm niForm = (NewInfoForm) form;
        
        // 查询当前页要显示的记录
        ParamNewInfoBean paramBean = new ParamNewInfoBean();
        List<ResultNewInfoBean> newInfoLst = 
                commonService.selRecordList(niForm, "newinfo.selNewInfoList", paramBean);
        request.setAttribute("newInfoLst", newInfoLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(paramBean, "newinfo.selNewInfoCount");
        niForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / niForm.getPageSize();
        if(totleCnt % niForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        niForm.setTotlePage(pageCnt);
        log.debug("查询新鲜资讯记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 添加记录
     * @create 2013-1-20 下午8:08:50
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        NewInfoForm niForm = (NewInfoForm) form;
        
        LoginUserInfo loginUserInfo = 
                (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        niForm.setCreateUser(loginUserInfo.getLoginName());
        
        int newId = newInfoService.insertNewInfo(niForm);
        log.debug("新增新鲜资讯内容ID=[" + newId + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * 删除单条记录
     * @create 2013-1-20 下午8:20:04
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        NewInfoForm niForm = (NewInfoForm) form;
        
        int id = niForm.getId();
        log.debug("新鲜资讯内容删除，要删除的记录ID=[" + id + "]");
        int delCnt = newInfoService.deleteOneNewInfo(id);
        log.debug("新鲜资讯[" + id + "]删除，影响行数=[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 批量删除
     * @create 2013-1-20 下午8:27:14
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        NewInfoForm niForm = (NewInfoForm) form;
        Integer[] ids = niForm.getIds();
        
        int delCnt = newInfoService.deleteManyNewInfo(ids);
        log.debug("批量新鲜资讯内容删除，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 打开内容编辑页面
     * @create 2013-1-20 下午8:56:33
     */
    public ActionForward detailForEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        NewInfoForm niForm = (NewInfoForm) form;
        int id = niForm.getId();
        ResultNewInfoBean result = newInfoService.getNewInfoDetail(id);
        
        request.setAttribute("newInfoDetail", result);
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑后的内容更新
     * @create 2013-1-20 下午10:30:47
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        NewInfoForm niForm = (NewInfoForm) form;
        int updCnt = newInfoService.updateNewInfo(niForm);
        log.debug("修改新鲜资讯[" + niForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * Portal页面查询使用，获取要在首页显示的新鲜资讯内容记录
     * @create 2013-2-2 上午12:56:03
     */
    public ActionForward portalLst(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        NewInfoForm niForm = (NewInfoForm) form;
        
        // 查询当前页要显示的记录
        ParamNewInfoBean paramBean = new ParamNewInfoBean();
        List<ResultNewInfoBean> newInfoLst = 
                commonService.selRecordList(niForm, "newinfo.selPortalNewInfoList", paramBean);
        request.setAttribute("newInfoLst", newInfoLst);
        return mapping.findForward("portalLst");
    }
    
    /**
     * 导航菜单：新鲜资讯列表
     * @create 2013-2-16 下午8:13:50
     */
    public ActionForward newinfolst(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        // 设置当前位置路径
        PortalLocalBean firLocalBean = new PortalLocalBean();
        firLocalBean.setShowName("新鲜资讯");
        firLocalBean.setHref("./newinfolst.do");
        request.setAttribute("firLocalBean", firLocalBean);
        
        NewInfoForm niForm = (NewInfoForm) form;
        
        // 查询当前页要显示的记录
        ParamNewInfoBean paramBean = new ParamNewInfoBean();
        List<ResultNewInfoBean> newInfoLst = 
                commonService.selRecordList(niForm, "newinfo.selPortalNewInfoList", paramBean);
        request.setAttribute("newInfoLst", newInfoLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(paramBean, "newinfo.selNewInfoCountForPortal");
        niForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / niForm.getPageSize();
        if(totleCnt % niForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        niForm.setTotlePage(pageCnt);
        log.debug("查询新鲜资讯记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("navLst");
    }
    
    /**
     * 打开内容详情页面
     * @create 2013-2-22 22:02:33
     */
    public ActionForward portalDetail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        // 设置当前位置路径
        PortalLocalBean firLocalBean = new PortalLocalBean();
        firLocalBean.setShowName("新鲜资讯");
        firLocalBean.setHref("./newinfolst.do");
        request.setAttribute("firLocalBean", firLocalBean);
        
        NewInfoForm niForm = (NewInfoForm) form;
        int id = niForm.getId();
        ResultNewInfoBean result = newInfoService.getNewInfoDetail(id);
        
        request.setAttribute("newInfoDetail", result);
        return mapping.findForward("detail");
    }

    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }

    public void setNewInfoService(NewInfoService newInfoService) {
        this.newInfoService = newInfoService;
    }
    
}
